package com.sbp.message.service.impl;

import com.dap.dao.DaoSupport;
import com.sbp.message.api.entity.LocalRecordStatus;
import com.sbp.message.api.entity.LocalRecordType;
import com.sbp.message.entity.ConsumerMessageRecord;
import com.sbp.message.service.ConsumerMessageRecordService;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class ConsumerMessageRecordServiceImpl implements ConsumerMessageRecordService {

    private DaoSupport daoService;
    public void setDaoService(DaoSupport daoService) {
        this.daoService = daoService;
    }


    @Override
    public void insert(ConsumerMessageRecord record) {
        record.setType(LocalRecordType.consumer.getValue());
        daoService.insert(record);
    }

    @Override
    public int updateStatus(String messageId, String group, LocalRecordStatus status) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("messageId", messageId);
        paramMap.put("appId", group);
        paramMap.put("status", status.getValue());
        return daoService.update(ConsumerMessageRecord.class, "updateStatusByMessageIdAndAppId", paramMap);
    }

    @Override
    public ConsumerMessageRecord selectOne(String messageId, String group) {
        if (StringUtils.isEmpty(messageId))
            return null;

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("messageId", messageId);
        paramMap.put("appId", group);
        return daoService.selectOne(ConsumerMessageRecord.class, "selectByMessageIdAndAppId", paramMap);
    }

    @Override
    public void delete(String messageId, String group) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("messageId", messageId);
        paramMap.put("appId", group);
        daoService.delete(ConsumerMessageRecord.class, "deleteByMessageIdAndAppId", paramMap);
    }

    public int deleteRecordBeforeTime(Date oldestTime, Set<String> destinationSet) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("oldestTime", oldestTime);
        paramMap.put("destinationSet", destinationSet);
        return daoService.delete(ConsumerMessageRecord.class, "deleteRecordBeforeTime", paramMap);
    }

}
